package main

import (
	"dxxgin/config"
	"dxxgin/models"
	"dxxgin/routers"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/gin-gonic/gin"
)

// 日志
func init() {
	logFile, err := os.OpenFile("./log.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("打开日志文件失败, err:", err)
		return
	}
	multiWriter := io.MultiWriter(os.Stdout, logFile)
	log.SetOutput(multiWriter)
}

func main() {
	// 初始化配置文件
	config.InitConf()

	// 初始化数据库
	mysqlConfigs := make(map[string]models.DBConfig)
	mysqlConfigs["daxinxinbase"] = models.DBConfig{
		Host:         config.Confok.Daxinxinbase.Host,
		Port:         config.Confok.Daxinxinbase.Port,
		User:         config.Confok.Daxinxinbase.User,
		Password:     config.Confok.Daxinxinbase.Pwd,
		Database:     config.Confok.Daxinxinbase.Database,
		Charset:      config.Confok.Daxinxinbase.Charset,
		MaxIdleConns: config.Confok.Database.MaxIdleConns,
		MaxOpenConns: config.Confok.Database.MaxOpenConns,
	}
	mysqlConfigs["tenders"] = models.DBConfig{
		Host:         config.Confok.Tenders.Host,
		Port:         config.Confok.Tenders.Port,
		User:         config.Confok.Tenders.User,
		Password:     config.Confok.Tenders.Pwd,
		Database:     config.Confok.Tenders.Database,
		Charset:      config.Confok.Tenders.Charset,
		MaxIdleConns: config.Confok.Database.MaxIdleConns,
		MaxOpenConns: config.Confok.Database.MaxOpenConns,
	}
	models.InitMysql(mysqlConfigs)

	// 路由
	r := gin.Default()
	routers.RegisterRoute(r)

	r.Run(":5588")
}
